//
//  ThemeManager.m
//  taptap-server
//
//  Created by Michael Humphris on 11/08/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ThemeManager.h"

@implementation ThemeManager
@synthesize delegate;
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

-(void)processDirectory:(NSString *)ThemePath{
    workerArgs* args = [[workerArgs alloc] initWithFolder:ThemePath isContainingFolder:YES];
    [self performSelectorInBackground:@selector(DoWork:) withObject:args];
    [args autorelease];
}

-(void)DoWork:(workerArgs *)args{
    NSMutableArray* themePaths = [[NSMutableArray alloc] init];
    if (![args isContainingFolder]) {
        [themePaths addObject:args.folder];
    } else {
        [themePaths addObjectsFromArray:[Filesystem directoriesForPath:[args folder]]];
        //[themePaths addObjectsFromArray:[[NSFileManager defaultManager] subpathsOfDirectoryAtPath: error:nil]];
    }
    
    uint count = 0;
    for (NSString* selectedPath in themePaths){
        if ([self isValidTheme:selectedPath]) {
            NSString* zipPath = [self makePathToZip:selectedPath];
            if (![zipPath hasSuffix:@".zip"]) {
                NSLog(@"Tried to save to non zip filename. This should never happen... EVER!");
            } else {
                ZipArchive *zip = [[ZipArchive alloc] init];
                [zip CreateZipFile2:zipPath];
                
                for (NSString* file in [Filesystem filesInDirectory:selectedPath isRecursive:YES]){
                    [zip addFileToZip:file newname:[file stringByReplacingOccurrencesOfString:selectedPath withString:@""]];
                }  
                [zip CloseZipFile2];
                [zip release];
            }
            
            
        } else {
            NSLog(@"Invalid Tap Tap Theme Selected: %@",selectedPath);
        }
        count++;
        float progress = ((float)count / (float)[themePaths count]) * (float)100;
        [delegate updateProgress:progress sender:self];
    }
    [delegate workDone:self];
}

-(BOOL)isValidTheme:(NSString *)path{
    NSString* infoPath = [Path Combine:path path2:@"Info.plist"];
    return [[NSFileManager defaultManager] fileExistsAtPath:infoPath]; 
}
-(NSString *)getNameOfTheme:(NSString *)path{
    return [[path lastPathComponent] stringByDeletingPathExtension];
}
-(int)getThemeVersion:(NSString *)path{
    NSString *infoPath = [Path Combine:path path2:@"Info.plist"];
    NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:infoPath];
    
    NSNumber *version  = [info objectForKey:@"kTTRDownloadableItemVersionKey"];
    
    return [version intValue];
}
-(NSString *)makePathToZip:(NSString *)path{
    NSString* themePath = @"~/Library/Application Support/TapTapServer/themes";
    themePath = [themePath stringByExpandingTildeInPath];

    
    
    if (![Filesystem directoryExists:themePath]) {
        [Filesystem createDirectory:themePath];
    }
    
    themePath = [Path Combine:themePath path2:[NSString stringWithFormat: @"%@Bundle",[self getNameOfTheme:path]]];
    if(![Filesystem directoryExists:themePath]){[Filesystem createDirectory:themePath];}
    
    themePath = [Path Combine:themePath path2:[NSString stringWithFormat:@"v%i",[self getThemeVersion:path]]];
    if(![Filesystem directoryExists:themePath]){[Filesystem createDirectory:themePath];}
    
    themePath = [Path Combine:themePath path2:[NSString stringWithFormat:@"%@.ttrTheme.zip",[self getNameOfTheme:path]]];
    return themePath;
    
}
@end
